import { store } from '@/store'
import { defineStore } from 'pinia'
import { getAccessToken, removeToken } from '@/utils/auth'
import { CACHE_KEY, deleteUserCache, useCache } from '@/hooks/web/useCache'
import { getInfo, loginOut } from '@/api/login'
import { ImrStudentVO } from '@/api/core/imrstudent'
import { AccountVO } from '@/api/system/account'
import { ustcRouters } from '@/router/modules/ustc'
import { imrRouters } from '@/router/modules/imr'
import { UstcStudentInfoView } from '@/types/ustcstudent'

const { wsCache } = useCache()

export type UserVO = Partial<ImrStudentVO | UstcStudentInfoView>

interface UserInfoVO {
  // USER 缓存
  isSetUser: boolean
  account: Partial<AccountVO>
  user: UserVO
}

export const useUserStore = defineStore('student-user', {
  state: (): UserInfoVO => ({
    isSetUser: false,
    account: {
      id: 0,
      username: '',
      nickname: '',
      phone: '',
      avatar: '',
      accountType: 1,
      activated: false,
      status: 0,
      loginIp: '',
      loginDate: new Date()
    },
    user: {}
  }),
  getters: {
    getIsSetUser(): boolean {
      return this.isSetUser
    },
    getAccount(): Partial<AccountVO> {
      return this.account
    },
    getUser(): UserVO {
      return this.user
    }
  },
  actions: {
    async setUserInfoAction() {
      if (!getAccessToken()) {
        this.resetState()
        return null
      }
      let userInfo = wsCache.get(CACHE_KEY.USER)
      if (!userInfo) {
        userInfo = await getInfo()
      }
      this.account = userInfo.account
      this.user = userInfo.user
      this.isSetUser = true

      let routers: AppCustomRouteRecordRaw[] = []
      // 根据用户信息初始化路由
      if (userInfo && userInfo.account?.accountType === 1) {
        routers = ustcRouters
      } else if (userInfo && userInfo.account?.accountType === 2) {
        routers = imrRouters
      }
      wsCache.set(CACHE_KEY.ROLE_ROUTERS, routers)
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async setUserAvatarAction(avatar: string) {
      const userInfo = wsCache.get(CACHE_KEY.USER)
      // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
      this.account.avatar = avatar
      userInfo.account.avatar = avatar
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async setUserNicknameAction(nickname: string) {
      const userInfo = wsCache.get(CACHE_KEY.USER)
      // NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
      this.account.nickname = nickname
      userInfo.account.nickname = nickname
      wsCache.set(CACHE_KEY.USER, userInfo)
    },
    async loginOut() {
      await loginOut()
      removeToken()
      deleteUserCache() // 删除用户缓存
      this.resetState()
    },
    resetState() {
      this.isSetUser = false
      this.account = {
        id: 0,
        username: '',
        nickname: '',
        phone: '',
        avatar: '',
        accountType: 1,
        activated: false,
        status: 0,
        loginIp: '',
        loginDate: new Date()
      }
      this.user = {}
    }
  }
})

export const useUserStoreWithOut = () => {
  return useUserStore(store)
}
